﻿using System;
using System.IO;

namespace GingerGL.IO
{
    //---------------------------------------------------------------//
    /// <summary>
    /// Virtual Folder File System
    /// </summary>
    public class FolderFileSystem : IFileSystem
    {
        //-----------------------------------------------------------//
        #region Variables
        private string m_path;
        private IFileSystem m_parent;
        #endregion
        //-----------------------------------------------------------//
        #region Properties
        /// <summary>
        /// Gets Folder Root Path
        /// </summary>
        public string Root
        {
            get { return m_path; }
        }
        /// <summary>
        /// Gets File System Parent
        /// </summary>
        public IFileSystem Parent
        {
            get { return m_parent; }
        }
        #endregion
        //-----------------------------------------------------------//
        #region Constructors
        /// <summary>
        /// Initailizes a new instance of object
        /// </summary>
        public FolderFileSystem(IFileSystem parent, string path)
        {
            m_parent = parent;
            m_path = path;
        }
        /// <summary>
        /// Initailizes a new instance of object
        /// </summary>
        public FolderFileSystem(string path)
        {

        }
        #endregion
        //-----------------------------------------------------------//
        #region Methods
        /// <summary>
        /// Opens File
        /// </summary>
        public Stream Open(string filename)
        {
            return m_parent.Open(GetPath(filename));
        }
        /// <summary>
        /// Returns True if file exists
        /// </summary>
        public bool Exists(string filename)
        {
            return m_parent.Exists(GetPath(filename));
        }
        /// <summary>
        /// Gets Fullpath
        /// </summary>
        public string GetFullPath(string filename)
        {
            return m_parent.GetFullPath(m_path + filename);
        }
        /// <summary>
        /// Gets a list of files
        /// </summary>
        public string[] GetFiles(string path)
        {
            return RemoveFolderPath(m_parent.GetFiles(Path.Merge(m_path, path)));
        }
        /// <summary>
        /// Gets a list of files
        /// </summary>
        public string[] GetFiles(string path, string pattern)
        {
            return RemoveFolderPath(m_parent.GetFiles(Path.Merge(m_path, path), pattern));
        }

        /// <summary>
        /// Gets a list of directories
        /// </summary>
        public string[] GetDirectories(string path)
        {
            return RemoveFolderPath(m_parent.GetDirectories(Path.Merge(m_path, path)));
        }
        /// <summary>
        /// Gets a list of directories
        /// </summary>
        public string[] GetDirectories(string path, string pattern)
        {
            return RemoveFolderPath(m_parent.GetDirectories(Path.Merge(m_path, path), pattern));
        }
        /// <summary>
        /// Gets the path using parent
        /// </summary>
        private string GetPath(string filename)
        {
            return Path.Merge(m_path, filename);
        }
        /// <summary>
        /// Removes Folder Information
        /// </summary>
        private string[] RemoveFolderPath(string[] path)
        {
            for (int ndex = 0; ndex < path.Length; ndex++)
            {
                path[ndex] = Path.Remove(path[ndex], m_path);
            }
            return path;
        }
        #endregion
        //-----------------------------------------------------------//
    }
    //---------------------------------------------------------------//
}
